#include "systemsettingspage.h"

SystemSettingsPage::SystemSettingsPage(QWidget *parent)
    : QWidget{parent}
{
    setupUI();
    setupConnections();
    applyStyling();
}

void SystemSettingsPage::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 15, 20, 15);
    mainLayout->setSpacing(25);

    // 添加各个设置模块
    mainLayout->addWidget(createGeneralSettingsGroup());
    mainLayout->addWidget(createAppearanceGroup());
    mainLayout->addWidget(createSoundSettingsGroup());

    mainLayout->addWidget(createSerialSettingsGroup());
    mainLayout->addWidget(createBottomControls());
    setLayout(mainLayout);
}

QGroupBox* SystemSettingsPage::createGeneralSettingsGroup()
{
    QGroupBox *group = new QGroupBox("常规设置", this);
    QFormLayout *layout = new QFormLayout(group);

    autoUpdateCheck = new QCheckBox("自动检查更新", group);
    saveHistoryCheck = new QCheckBox("保存历史记录", group);

    layout->addRow("系统更新:", autoUpdateCheck);
    layout->addRow("数据保存:", saveHistoryCheck);
    group->setLayout(layout);
    return group;
}

QGroupBox* SystemSettingsPage::createAppearanceGroup()
{
    QGroupBox *group = new QGroupBox("外观设置", this);
    QFormLayout *layout = new QFormLayout(group);

    themeCombo = new QComboBox(group);
    themeCombo->addItems({"浅色主题", "深色主题", "系统默认"});

    layout->addRow("界面主题:", themeCombo);
    return group;
}

QGroupBox* SystemSettingsPage::createSoundSettingsGroup()
{
    QGroupBox *group = new QGroupBox("声音设置", this);
    QFormLayout *layout = new QFormLayout(group);

    volumeSlider = new QSlider(Qt::Horizontal, group);
    volumeSlider->setRange(0, 100);
    volumeSlider->setValue(100);
    notificationCheck = new QCheckBox("启用通知音效", group);
    notificationCheck->setCheckState(Qt::Checked);
    layout->addRow("系统音量:", volumeSlider);
    layout->addRow("音效设置:", notificationCheck);

    connect(volumeSlider,SIGNAL(valueChanged(int)),this,SLOT(change_alarm_volumn(int)));
    connect(notificationCheck,SIGNAL(checkStateChanged(Qt::CheckState)),this,SLOT(sound_effect_check(Qt::CheckState)));
    return group;
}

void SystemSettingsPage::change_alarm_volumn(int value)
{
    if(summary!=nullptr)
    {
        summary->alarm_sound->setVolume(value*0.01);
    }
    else
    {
        qDebug()<<"设置面板的概括页面指针为空";
    }
}

 void SystemSettingsPage::sound_effect_check(Qt::CheckState state)
{
     if(state==Qt::Checked)
    {
        if(summary!=nullptr)
        {
            summary->alarm_sound->setVolume(volumeSlider->value()*0.01);
        }
        else
        {
            qDebug()<<"设置面板的概括页面指针为空";
        }
    }
    else if(state==Qt::Unchecked)
    {
         if(summary!=nullptr)
         {
             summary->alarm_sound->setVolume(0);
         }
         else
         {
             qDebug()<<"设置面板的概括页面指针为空";
         }
    }
}

QGroupBox* SystemSettingsPage::createSerialSettingsGroup()
{
    QGroupBox *group = new QGroupBox("串口设置", this);
    QFormLayout *layout = new QFormLayout(group);
    debug_data_save_location=QDir::currentPath()+"/Log";
    receive_data_save_location=QDir::currentPath()+"/Receive";
    receive_config_location=QDir::currentPath()+"/Receive_config";
    send_config_location=QDir::currentPath()+"/Send_config";
    QLabel *debug_data_save_location_label=new QLabel("日志数据保存地址:"+debug_data_save_location);
    QLabel *receive_data_save_location_label=new QLabel("接收区数据保存地址:"+receive_data_save_location);
    QLabel *receive_config_location_label=new QLabel("接收配置文件地址:"+receive_config_location);
    QLabel *send_config_location_label=new QLabel("发送配置文件地址:"+send_config_location);
    layout->addWidget(debug_data_save_location_label);
    layout->addWidget(receive_data_save_location_label);
    layout->addWidget(receive_config_location_label);
    layout->addWidget(send_config_location_label);
    group->setLayout(layout);
    return group;
}

QWidget* SystemSettingsPage::createBottomControls()
{
    QWidget *container = new QWidget(this);
    QHBoxLayout *layout = new QHBoxLayout(container);

    QPushButton *defaultsButton = new QPushButton("恢复默认", container);
    QPushButton *helpButton = new QPushButton("帮助", container);
    QPushButton *exitButton = new QPushButton("退出", container);

    layout->addWidget(defaultsButton);
    layout->addStretch();
    layout->addWidget(helpButton);
    layout->addWidget(exitButton);

    connect(defaultsButton, &QPushButton::clicked, this, &SystemSettingsPage::restoreDefaultsRequested);
    return container;
}

void SystemSettingsPage::setupConnections()
{
    connect(themeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &SystemSettingsPage::onThemeChanged);

    // 连接退出和帮助按钮
    QList<QPushButton*> buttons = findChildren<QPushButton*>();
    foreach (QPushButton *btn, buttons) {
        if (btn->text() == "退出") connect(btn, &QPushButton::clicked, this, &SystemSettingsPage::onExitClicked);
        if (btn->text() == "帮助") connect(btn, &QPushButton::clicked, this, &SystemSettingsPage::onHelpClicked);
    }
}

void SystemSettingsPage::applyStyling()
{
    // 设置调色板
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, QColor(245, 245, 245));
    palette.setColor(QPalette::WindowText, Qt::darkGray);
    setPalette(palette);

    // 控件样式设置
    foreach (QGroupBox *group, findChildren<QGroupBox*>()) {
        group->setStyleSheet("QGroupBox { border: 1px solid #E0E0E0; "
                             "border-radius: 5px; margin-top: 10px; }"
                             "QGroupBox::title { subcontrol-origin: margin; "
                             "left: 10px; color: #606060; }");
    }

    foreach (QPushButton *btn, findChildren<QPushButton*>()) {
        btn->setMinimumSize(80, 30);
        btn->setPalette(QPalette(Qt::white));
        btn->setStyleSheet("QPushButton { border: 1px solid #D0D0D0; "
                           "border-radius: 4px; padding: 5px; }"
                           "QPushButton:hover { background: #F0F0F0; }");
    }
}

void SystemSettingsPage::onExitClicked() { QApplication::quit(); }
void SystemSettingsPage::onHelpClicked() { emit helpRequested(); }
void SystemSettingsPage::onThemeChanged(int index) { emit themeChanged(index); }
